<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;


class Activity extends  Model
{

    const status1 = '报名未开始';
    const status2 = '报名进行中';
    const status3 = '报名已结束';
    const status4 = '活动未开始';
    const status5 = '活动进行中';
    const status6 = '活动已结束';

    use HasFactory;
    use SoftDeletes;

    protected $table = 'activity';
    static $unguarded = true;

    /**
     * 根据ID获取数据
     *
     * @param $id
     * @param mixed $columns
     * @return array|Builder
     */
    public static function getDataByID($id, string $columns = ''){
        $data = self::query();
        if($columns){
            $data->find($id,$columns);
            return $data;
        }
        $val = $data->find($id);
        if (empty($val)) {
            return [];
        }
        $status_show =  self::getStatusValue($val['status'],true);
        $signup_status_show = self::getStatusValue($val['signup_status']);
//        $status_html_show = getStatusHTMLValue($val['status']);
        $image = $val['image'];
        if($image){
            $image_path = getImageUrl($val['image']);
            $image_show = '<img width="60" height="60" src="'.$image_path.'">';
        }
        $username = '后台发布';
        if($val['user_id'] != null){
            $username = $val['username'];
        }
        return [
            'id'=>$val['id'],
            'name'=>$val['name'],
            'image'=>$image,
            'image_path'=>$image_path??"",
            'image_show'=>$image_show??"未上传",
            'signup_status'=>$val['signup_status'],
            'signup_status_show'=>$signup_status_show,
            'status'=>$val['status'],
            'status_show'=>$status_show,
            'sort'=>$val['sort'],
            'signup_start_at'=>$val['signup_start_at'],
            'signup_end_at'=>$val['signup_end_at'],
            'start_at'=>$val['start_at'],
            'end_at'=>$val['end_at'],
            'user_id'=>$val['user_id'],
            'username'=>$username,
//            'status_html_show'=>$status_html_show,
            'price'=>$val['price'],
            'contacts'=>$val['contacts'],
            'contact_mobile'=>$val['contact_mobile'],
            'detail'=>$val['detail'],
            'local'=>$val['local'],
            'total_counts'=>$val['total_counts'],
            'signup_counts'=>$val['signup_counts'],
            'remaining_counts'=>$val['remaining_counts'],
            'created_at'=>Carbon::parse($val['created_at'])->toDateTimeString(),
            'updated_at'=>Carbon::parse($val['updated_at'])->toDateTimeString(),
       ];
    }

    /**
     * 通过条件查找列表
     * 
     */
    public static function getActivityListByWhere($where,$field='*'){
        if(empty($where)){
            return [];
        }
        $data = self::query()->where($where)->latest('start_at')->oldest('sort')->select($field)->get();


        if(!empty($data)){
            foreach($data as $key=>$val){
                $data[$key]['image'] =  getImageUrl($val['image']);
            }
        }

        return $data;
    }


    /**
     * 格式化数据
     * 
     */
    public static function formatListData($data){
        if($data){
            //报名状态：1-未开始，2-报名中，3-已结束
            $data['signup_status_show'] = self::getStatusValue($data['signup_status']);
            $data['status_show'] = self::getStatusValue($data['status'],true);

            $image = $data['image'];
            if($image){
                $image_path = getImageUrl($data['image']);
                $image_show = '<img width="60" height="60" src="'.$image_path.'">';
            }
            $data['image_path'] = $image_path??"";
            $data['image_show'] = $image_show??"";
        }
        return $data;
    }

    public static function formatListDataOne($data){
        if($data){
            $data = json_encode($data);
            $data = json_decode($data,true);
            //报名状态：1-未开始，2-报名中，3-已结束
            $data['signup_status_show'] = self::getStatusValue($data['signup_status']);
            $data['status_show'] = self::getStatusValue($data['status'],true);

            $image = $data['image'];
            if($image){
                $image_path = getImageUrl($data['image']);
                $image_show = '<img width="60" height="60" src="'.$image_path.'">';
            }
            $data['image_path'] = $image_path??"";
            $data['image_show'] = $image_show??"";
        }
        return $data;
    }

    /**
     * 活动状态
     * @return string[]
     */
    public static function getStatusArray(): array
    {
        return [
            1=>self::status4,
            2=>self::status5,
            3=>self::status6,
        ];
    }

    /**
     * 报名状态
     * @return string[]
     */
    public static function getSignUpStatusArray(): array
    {
        return [
            1=>self::status1,
            2=>self::status2,
            3=>self::status3,
        ];
    }

    /**
     * 获取状态值
     *
     * @param $status
     * @param bool $activity
     * @return string
     */
    private static function getStatusValue($status, bool $activity = false): string
    {
        if($activity){
            return self::getStatusArray()[$status];
        }
        return self::getSignUpStatusArray()[$status];
    }

    /**
     * 根据时间计算状态:1-未开始，2-进行中，3-已结束
     *
     * @param $start_at
     * @param $end_at
     * @param bool $is_signUp 是否报名状态
     * @return string
     */
    public static function computeStatusByTime($start_at, $end_at, bool $is_signUp = false): string
    {
        $now = Carbon::now();
        $start_at = Carbon::parse($start_at);
        $end_at = Carbon::parse($end_at);
        $status = 1;
        if($now >= $start_at && $now <= $end_at){
            $status = 2;
        }
        if($now > $end_at){
            $status = 3;
        }
        return $status;
//        $status_value = self::getStatusArray()[$status];
//        if($is_signUp){
//            $status_value = self::getSignUpStatusArray()[$status];
//        }
//        return $status_value;
    }
}
